package com.goldman.http.common.s2s.model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class ResponseData<D> {

    private Status status;
    private D data;

    public boolean isSuccess() {
        if (status != null && status.getCode() == ErrorCode.SUCCESS.getCode()) {
            return true;
        }
        return false;
    }

    public boolean isError() {
        if (status != null && status.getCode() == ErrorCode.SUCCESS.getCode()) {
            return false;
        }
        return true;
    }

    public static <D> ResponseData<D> success(D d) {
        Status status = new Status(ErrorCode.SUCCESS, "");
        ResponseData responseData = new ResponseData<D>(status, d);
        return responseData;
    }

    public static <D> ResponseData<D> failed() {
        return newWhenErr(ErrorCode.FAILED.getCode(), ErrorCode.FAILED.getMsg(), "");
    }

    public static <D> ResponseData<D> failed(String err) {
        return newWhenErr(ErrorCode.FAILED.getCode(), ErrorCode.FAILED.getMsg(), err);
    }

    public static <D> ResponseData<D> failed(String msg, String err) {
        return newWhenErr(ErrorCode.FAILED.getCode(), msg, err);
    }

    public static <D> ResponseData<D> failed(IErrorCode errorCode, String err) {
        return newWhenErr(errorCode.getCode(), errorCode.getMsg(), err);
    }

    public static <D> ResponseData<D> failed(Integer errorCode, String meassage, String err) {
        return newWhenErr(errorCode, meassage, err);
    }


    public static <D> ResponseData<D> sysError() {
        return newWhenErr(ErrorCode.SYS_ERROR.getCode(), ErrorCode.SYS_ERROR.getMsg(), "");
    }

    public static <D> ResponseData<D> sysError(String err) {
        return newWhenErr(ErrorCode.SYS_ERROR.getCode(), ErrorCode.SYS_ERROR.getMsg(), err);
    }

    public static <D> ResponseData<D> sysError(String msg, String err) {
        return newWhenErr(ErrorCode.SYS_ERROR.getCode(), msg, err);
    }


    public static <D> ResponseData<D> argError() {
        return newWhenErr(ErrorCode.ARG_ERROR.getCode(), ErrorCode.ARG_ERROR.getMsg(), "");
    }

    public static <D> ResponseData<D> argError(String err) {
        return newWhenErr(ErrorCode.ARG_ERROR.getCode(), ErrorCode.ARG_ERROR.getMsg(), err);
    }

    public static <D> ResponseData<D> argError(String msg, String err) {
        return newWhenErr(ErrorCode.ARG_ERROR.getCode(), msg, err);
    }

    public static <D> ResponseData<D> newWhenErr(int code, String... msgs) {
        String msg = msgs != null && msgs.length > 0 ? msgs[0] : "";
        String err = msgs != null && msgs.length > 1 ? msgs[1] : "";
        Status status = new Status(code, msg, err);
        ResponseData responseData = new ResponseData<D>(status, null);
        return responseData;
    }

    public static <D> ResponseData<D> newWhenErr(Status status) {
        Status newStatus = new Status(status.getCode(), status.getMsg(), status.getErr());
        ResponseData responseData = new ResponseData<D>(newStatus, null);
        return responseData;
    }
}
